package opc.read.OpcQie;

import org.openscada.opc.lib.da.Item;

import java.util.*;

/**
 * @Description: 需要将itemSource即源数据切割
 * @author: liyuan
 * @data 2021-01-22 14:23
 */
public class ItemSourceQieUtil {
    private static Integer size = 5;
    public static List<Map<String, Map<String, Item>>> itemSourceZu=new ArrayList<>(size);

    public static List<Map<String, Map<String, Item>>> getItemSourceZu(Map<String, Map<String, Item>> itemSource) {
        //应该是个数加上1，再加上一个1（业务相关的）
        int sonSize = (int) itemSource.size() / size+2;
        Map<String, Map<String, Item>> map1 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
        Map<String, Map<String, Item>> map2 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
        Map<String, Map<String, Item>> map3 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
        Map<String, Map<String, Item>> map4 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
        Map<String, Map<String, Item>> map5 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
//        Map<String, Map<String, Item>> map6 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
//        Map<String, Map<String, Item>> map7 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
//        Map<String, Map<String, Item>> map8 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
//        Map<String, Map<String, Item>> map9 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
//        Map<String, Map<String, Item>> map10 = new LinkedHashMap<String, Map<String, Item>>(sonSize);
        int k = 1, j = 1;
        //孙子Map的大小
        int statisticSize = itemSource.get("statistic_total").size();
        int sunSize = statisticSize/ size+1;
        Map<String, Item> statistic_total = new LinkedHashMap<String, Item>(sunSize);
        Map<String, Item> statistic_tota2 = new LinkedHashMap<String, Item>(sunSize);
        Map<String, Item> statistic_tota3 = new LinkedHashMap<String, Item>(sunSize);
        Map<String, Item> statistic_tota4 = new LinkedHashMap<String, Item>(sunSize);
        Map<String, Item> statistic_tota5 = new LinkedHashMap<String, Item>(sunSize);
        for (Map.Entry<String, Map<String, Item>> entry : itemSource.entrySet()) {
            if (entry.getKey().equals("statistic_total")) {
                Map<String, Item> value = entry.getValue();
                for (Map.Entry<String, Item> sunMap : value.entrySet()) {
                    if(k==1){
                        statistic_total.put(sunMap.getKey(),sunMap.getValue());
                        k++;
                    }
                    else if(k==2){
                        statistic_tota2.put(sunMap.getKey(),sunMap.getValue());
                        k++;
                    }
                    else if(k==3){
                        statistic_tota3.put(sunMap.getKey(),sunMap.getValue());
                        k++;
                    }
                    else if(k==4){
                        statistic_tota4.put(sunMap.getKey(),sunMap.getValue());
                        k++;
                    }
                    else if(k==5){
                        statistic_tota5.put(sunMap.getKey(),sunMap.getValue());
                        k=1;
                    }
                }
                map1.put("statistic_total",statistic_total);
                map2.put("statistic_total",statistic_tota2);
                map3.put("statistic_total",statistic_tota3);
                map4.put("statistic_total",statistic_tota4);
                map5.put("statistic_total",statistic_tota5);

            } else {
                if (j == 1) {
                    map1.put(entry.getKey(), entry.getValue());
                    j++;
                }
                else if (j == 2) {
                    map2.put(entry.getKey(), entry.getValue());
                    j++;
                }
                else if (j == 3) {
                    map3.put(entry.getKey(), entry.getValue());
                    j++;
                }
                else if (j == 4) {
                    map4.put(entry.getKey(), entry.getValue());
                    j++;
                }
                else if (j == 5) {
                    map5.put(entry.getKey(), entry.getValue());
                    j = 1;
                }

            }
        }
        itemSourceZu.add(map1);
        itemSourceZu.add(map2);
        itemSourceZu.add(map3);
        itemSourceZu.add(map4);
        itemSourceZu.add(map5);
        return itemSourceZu;
    }

}
